{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tool Use Chain of Thought (CoT) Enhancement\n",
    "\n",
    "This notebook demonstrates how to prepare and enhance tool-use conversations with Chain of Thought (CoT) reasoning using Synthetic Data Kit.\n",
    "\n",
    "## What We'll Do:\n",
    "\n",
    "1. Load a tool-use dataset from Hugging Face\n",
    "2. Format the data to include proper tool tags (`<tool></tool>`)\n",
    "3. Create a small sample for demonstration\n",
    "4. Generate a configuration file for CoT enhancement\n",
    "5. Use the `cot-enhance` feature to add detailed reasoning to tool calls\n",
    "6. Compare original vs. enhanced conversations\n",
    "\n",
    "## Why CoT Enhancement Matters:\n",
    "\n",
    "Chain of Thought enhancement makes the model's reasoning explicit before making tool calls. This helps:\n",
    "- Make the model's decision process more transparent\n",
    "- Provide better training examples for fine-tuning\n",
    "- Improve model performance on complex reasoning tasks\n",
    "- Create datasets that encourage step-by-step thinking\n",
    "\n",
    "Let's get started!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c4b2b9b7-acab-46a8-bdf5-ce196c993323",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets import load_from_disk, load_dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "180d2a43-6e1e-4965-9d71-8238b84b2cfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = load_dataset(\"Team-ACE/ToolACE\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2f215f7b-023d-4cfe-972c-5c36986127a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatasetDict({\n",
       "    train: Dataset({\n",
       "        features: ['system', 'conversations'],\n",
       "        num_rows: 11300\n",
       "    })\n",
       "})"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "681daa0b-c26a-4dea-8b96-b729beede1bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['system', 'conversations'],\n",
       "    num_rows: 11300\n",
       "})"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset['train']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "412b5d50-41fa-428d-aaa4-8ee17cae6c9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'system': 'You are an expert in composing functions. You are given a question and a set of possible functions. \\nBased on the question, you will need to make one or more function/tool calls to achieve the purpose. \\nIf none of the function can be used, point it out. If the given question lacks the parameters required by the function,\\nalso point it out. You should only return the function call in tools call sections.\\nHere is a list of functions in JSON format that you can invoke:\\n[{\"name\": \"newAddress\", \"description\": \"Generates a new Ethereum address that can be used to send or receive funds. Do not lose the password! We can\\'t restore access to an address if you lose it.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"password\": {\"description\": \"The password for the new Ethereum address\", \"type\": \"string\"}}, \"required\": [\"password\"]}, \"required\": null}, {\"name\": \"Market Trends API\", \"description\": \"Get the latest market trends and relevant news for a specified country and language.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"trend_type\": {\"description\": \"Trend type.\", \"type\": \"string\", \"enum\": [\"MARKET_INDEXES\", \"MOST_ACTIVE\", \"GAINERS\", \"LOSERS\", \"CRYPTO\", \"CURRENCIES\", \"CLIMATE_LEADERS\"]}, \"country\": {\"description\": \"The country for which to get trends, specified as a 2-letter country code - see ISO 3166.\", \"type\": \"string\", \"default\": \"us\"}, \"language\": {\"description\": \"The language to use for the results, specified as a 2-letter language code - see ISO 639-1.\", \"type\": \"string\", \"default\": \"en\"}}, \"required\": [\"trend_type\"]}, \"required\": null}, {\"name\": \"Get Futures Prices\", \"description\": \"Retrieve a list of current futures prices for various financial instruments.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"instrument_type\": {\"description\": \"Type of financial instrument (e.g., commodity, currency, index)\", \"type\": \"string\"}, \"exchange\": {\"description\": \"Name of the exchange (e.g., CME, ICE, NYMEX)\", \"type\": \"string\"}, \"start_string\": {\"description\": \"Start string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}, \"end_string\": {\"description\": \"End string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}}, \"required\": [\"instrument_type\", \"exchange\"]}, \"required\": null}, {\"name\": \"SEC Filings\", \"description\": \"Returns a list of SEC Filings for the requested company, including filing types, strings, and documents.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"identifier\": {\"description\": \"Publicly traded company\\'s stock symbol or Central Index Key (CIK)\", \"type\": \"string\", \"default\": \"aapl\"}}, \"required\": [\"identifier\"]}, \"required\": null}, {\"name\": \"United States Away from Home Mobility API\", \"description\": \"Retrieve daily data on the percentage change in time spent away from home in the United States, providing insights into the economic impact of the COVID-19 pandemic.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"string\": {\"description\": \"The string for which to retrieve data (format: YYYY-MM-DD)\", \"type\": \"string\"}, \"state\": {\"description\": \"The state for which to retrieve data (optional, default: all states)\", \"type\": \"string\"}}, \"required\": [\"string\"]}, \"required\": null}, {\"name\": \"Calculate Sales Tax\", \"description\": \"Retrieves the sales tax rate applicable to a specific address. This API accepts address inputs to deliver up-to-string, relevant local sales tax rates instantly.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"country\": {\"description\": \"Set to one of the country codes listed in Supported Countries.\", \"type\": \"string\", \"default\": \"US\"}, \"city\": {\"description\": \"City name\", \"type\": \"string\", \"default\": \"Meridian\"}, \"zip\": {\"description\": \"Zip code\", \"type\": \"string\", \"default\": \"83646\"}, \"street\": {\"description\": \"Street address\", \"type\": \"string\", \"default\": \"936 Storey Ave\"}}, \"required\": [\"country\"]}, \"required\": null}]. \\nShould you decide to return the function call(s). \\nPut it in the format of [func1(params_name=params_value, params_name2=params_value2...), func2(params)]\\n\\nNO other text MUST be included. \\n',\n",
       " 'conversations': [{'from': 'user',\n",
       "   'value': \"I'm considering investing and I'd like to know what's happening in the market right now. Could you get me the top market trends in the US?\"},\n",
       "  {'from': 'assistant',\n",
       "   'value': '[Market Trends API(trend_type=\"MARKET_INDEXES\", country=\"us\")]'},\n",
       "  {'from': 'tool',\n",
       "   'value': '[{\"name\": \"Market Trends API\", \"results\": {\"trends\": [{\"name\": \"S&P 500\", \"description\": \"Standard & Poor\\'s 500 Index is a market-capitalization-weighted index of the 500 largest U.S. publicly traded companies.\", \"data\": {\"current_value\": \"4172.80\", \"percentage_change\": \"+0.68%\"}}, {\"name\": \"DOW J\", \"description\": \"Dow Jones Industrial Average is a price-weighted average of 30 blue-chip stocks that are generally the leaders in their industry.\", \"data\": {\"current_value\": \"34479.60\", \"percentage_change\": \"+0.47%\"}}, {\"name\": \"NASDAQ\", \"description\": \"The NASDAQ Composite is a broad-based capitalization-weighted index of stocks in all three NASDAQ tiers: Global Select, Global Market and Capital Market.\", \"data\": {\"current_value\": \"13691.30\", \"percentage_change\": \"+0.90%\"}}]}}]'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"Here are the top Market Trends in the US right now:\\n\\n1. **S&P 500**: The Standard & Poor's 500 Index is a market-capitalization-weighted index of the 500 largest U.S. publicly traded companies. Its current value is 4172.80 with a percentage change of +0.68%.\\n\\n2. **DOW J**: The Dow Jones Industrial Average is a price-weighted average of 30 blue-chip stocks that are generally the leaders in their industry. Its current value is 34479.60 with a percentage change of +0.47%.\\n\\n3. **NASDAQ**: The NASDAQ Composite is a broad-based capitalization-weighted index of stocks in all three NASDAQ tiers: Global Select, Global Market and Capital Market. Its current value is 13691.30 with a percentage change of +0.90%.\\n\\nThis information can help you make informed decisions about your investment plans.\"},\n",
       "  {'from': 'user',\n",
       "   'value': 'Could you explain what SEC filings are and how they might be useful for an investor like me?'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"Sure, SEC filings refer to official documents that are submitted by publicly traded companies to the U.S. Securities and Exchange Commission (SEC). These documents provide valuable information about a company's financial performance, its management team, ownership, key business operations, market competition, and future prospects. They are typically comprehensive and contain detailed information that a company is legally required to disclose to the public.\\n\\nInvestors like you can use these filings to better understand a company's true financial health and operations. Information you might glean from SEC filings includes:\\n\\n1. **Financial Statements**: Balance sheets, income statements, and cash flow statements can help you analyze the financial health and profitability of a company.\\n\\n2. **Management Discussion and Analysis (MD&A)**: This section offers management's perspective on the company's performance, including explanations of financial results, significant changes, market risk exposures, and future operations and strategy.\\n\\n3. **Risk Factors**: In the filing, companies are required to disclose potential risks of investing in their company. Insight into the risks the company is facing can help you make a more informed decision about investing.\\n\\n4. **Major Shareholders**: Companies must disclose the list of major shareholders. High ownership of shares by insiders could indicate confidence in the business, while large institutional ownership could suggest stability.\\n\\n5. **Director and Executive Compensation**: Information on executive remuneration packages can indicate how well the company’s interests are aligned with its executive team.\\n\\nSEC filings are a fundamental part of investment research and due diligence. They can provide deep insights into a company that may not be immediately obvious from simply looking at its earnings or share price. It's essential for investors to understand how to read and interpret these filings to make informed investment decisions.\"},\n",
       "  {'from': 'user',\n",
       "   'value': 'So, a couple of years ago, I invested in a tech startup without doing any proper research. It seemed like a hot trend at the time, but the company went belly up within a year. I ended up losing a good chunk of money. What are your thoughts on that?'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"As an assistant, I'm here to provide advice and guide you in your financial decisions. Here are my thoughts:\\n\\nInvesting in startups or any company without conducting proper research carries high risk. Although some startups have very high growth potentials and can provide excellent returns, many also fail within a few years of operation. This is especially the case in the tech industry where competition is stiff and technology changes rapidly.\\n\\nThat being said, your past experience with the tech startup is an important lesson in the necessity of conducting thorough due diligence before investing. Here are some steps you might take before future investments:\\n\\n1. **Understand the Business**: Make sure you understand the company's business model, its products or services, the market it operates in and its competition.\\n\\n2. **Financial Analysis**: Review financial statements, revenue, profitability, growth rates, debt levels, and cash flow. \\n\\n3. **Management Review**: It's important to assess the quality of the management team and their plans for the company.\\n\\n4. **Use of Tools**: Utilize tools like the SEC Filings tool to retrieve important documents to get detailed information about a company's operations, management and financials.\\n\\n5. **Risk vs Reward**: Analyze the investment's potential returns in light of the potential risk. High-risk investments should have the potential for higher returns.\\n\\nRemember, all investments carry a degree of risk. As such, it can be beneficial to diversify investments to spread risk and not put all of your money into one company or sector. It may also be helpful to seek advice from financial advisors or use trusted financial services to ensure informed decisions.\\n\\nGoing forward, employing these strategies could help reduce risk and maximize potential returns on your investments.\"},\n",
       "  {'from': 'user',\n",
       "   'value': \"Could you provide me with the SEC filings for Apple? Also, I’m curious about how COVID-19 has affected people's mobility away from home in the US.\"},\n",
       "  {'from': 'assistant',\n",
       "   'value': '[SEC Filings(identifier=\"AAPL\"), United States Away from Home Mobility API(string=\"2025-05-17\")]'}]}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset['train'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a63191fa-a89d-4e2a-9bb6-2f3c840b1445",
   "metadata": {},
   "outputs": [],
   "source": [
    "cot = load_from_disk(\"./CoT_ToolACE/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dfb66b7c-bbec-4fd6-b078-f120cb5ba946",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': '5c1710db-5977-4fb4-b5e2-edc83c34d761',\n",
       " 'conversations': [{'from': 'system',\n",
       "   'value': 'You are ToolLlama, whenever asked a question, requiring tool call you responed after thinking and returning tool call within <tool></tool> XML tags.\\nYou are an expert in composing functions. You are given a question and a set of possible functions. \\nBased on the question, you will need to make one or more function/tool calls to achieve the purpose. \\nIf none of the function can be used, point it out. If the given question lacks the parameters required by the function,\\nalso point it out. .\\nHere is a list of functions in JSON format that you can invoke:\\n[{\"name\": \"newAddress\", \"description\": \"Generates a new Ethereum address that can be used to send or receive funds. Do not lose the password! We can\\'t restore access to an address if you lose it.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"password\": {\"description\": \"The password for the new Ethereum address\", \"type\": \"string\"}}, \"required\": [\"password\"]}, \"required\": null}, {\"name\": \"Market Trends API\", \"description\": \"Get the latest market trends and relevant news for a specified country and language.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"trend_type\": {\"description\": \"Trend type.\", \"type\": \"string\", \"enum\": [\"MARKET_INDEXES\", \"MOST_ACTIVE\", \"GAINERS\", \"LOSERS\", \"CRYPTO\", \"CURRENCIES\", \"CLIMATE_LEADERS\"]}, \"country\": {\"description\": \"The country for which to get trends, specified as a 2-letter country code - see ISO 3166.\", \"type\": \"string\", \"default\": \"us\"}, \"language\": {\"description\": \"The language to use for the results, specified as a 2-letter language code - see ISO 639-1.\", \"type\": \"string\", \"default\": \"en\"}}, \"required\": [\"trend_type\"]}, \"required\": null}, {\"name\": \"Get Futures Prices\", \"description\": \"Retrieve a list of current futures prices for various financial instruments.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"instrument_type\": {\"description\": \"Type of financial instrument (e.g., commodity, currency, index)\", \"type\": \"string\"}, \"exchange\": {\"description\": \"Name of the exchange (e.g., CME, ICE, NYMEX)\", \"type\": \"string\"}, \"start_string\": {\"description\": \"Start string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}, \"end_string\": {\"description\": \"End string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}}, \"required\": [\"instrument_type\", \"exchange\"]}, \"required\": null}, {\"name\": \"SEC Filings\", \"description\": \"Returns a list of SEC Filings for the requested company, including filing types, strings, and documents.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"identifier\": {\"description\": \"Publicly traded company\\'s stock symbol or Central Index Key (CIK)\", \"type\": \"string\", \"default\": \"aapl\"}}, \"required\": [\"identifier\"]}, \"required\": null}, {\"name\": \"United States Away from Home Mobility API\", \"description\": \"Retrieve daily data on the percentage change in time spent away from home in the United States, providing insights into the economic impact of the COVID-19 pandemic.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"string\": {\"description\": \"The string for which to retrieve data (format: YYYY-MM-DD)\", \"type\": \"string\"}, \"state\": {\"description\": \"The state for which to retrieve data (optional, default: all states)\", \"type\": \"string\"}}, \"required\": [\"string\"]}, \"required\": null}, {\"name\": \"Calculate Sales Tax\", \"description\": \"Retrieves the sales tax rate applicable to a specific address. This API accepts address inputs to deliver up-to-string, relevant local sales tax rates instantly.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"country\": {\"description\": \"Set to one of the country codes listed in Supported Countries.\", \"type\": \"string\", \"default\": \"US\"}, \"city\": {\"description\": \"City name\", \"type\": \"string\", \"default\": \"Meridian\"}, \"zip\": {\"description\": \"Zip code\", \"type\": \"string\", \"default\": \"83646\"}, \"street\": {\"description\": \"Street address\", \"type\": \"string\", \"default\": \"936 Storey Ave\"}}, \"required\": [\"country\"]}, \"required\": null}]. \\nShould you decide to return the function call(s). \\nPut it in the format of [func1(params_name=params_value, params_name2=params_value2...), func2(params)]\\n\\n. \\n'},\n",
       "  {'from': 'user',\n",
       "   'value': \"I'm considering investing and I'd like to know what's happening in the market right now. Could you get me the top market trends in the US?\"},\n",
       "  {'from': 'assistant',\n",
       "   'value': '<tool>[Market Trends API(trend_type=\"MARKET_INDEXES\", country=\"us\")]</tool>'},\n",
       "  {'from': 'tool',\n",
       "   'value': '[{\"name\": \"Market Trends API\", \"results\": {\"trends\": [{\"name\": \"S&P 500\", \"description\": \"Standard & Poor\\'s 500 Index is a market-capitalization-weighted index of the 500 largest U.S. publicly traded companies.\", \"data\": {\"current_value\": \"4172.80\", \"percentage_change\": \"+0.68%\"}}, {\"name\": \"DOW J\", \"description\": \"Dow Jones Industrial Average is a price-weighted average of 30 blue-chip stocks that are generally the leaders in their industry.\", \"data\": {\"current_value\": \"34479.60\", \"percentage_change\": \"+0.47%\"}}, {\"name\": \"NASDAQ\", \"description\": \"The NASDAQ Composite is a broad-based capitalization-weighted index of stocks in all three NASDAQ tiers: Global Select, Global Market and Capital Market.\", \"data\": {\"current_value\": \"13691.30\", \"percentage_change\": \"+0.90%\"}}]}}]'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"Here are the top Market Trends in the US right now:\\n\\n1. **S&P 500**: The Standard & Poor's 500 Index is a market-capitalization-weighted index of the 500 largest U.S. publicly traded companies. Its current value is 4172.80 with a percentage change of +0.68%.\\n\\n2. **DOW J**: The Dow Jones Industrial Average is a price-weighted average of 30 blue-chip stocks that are generally the leaders in their industry. Its current value is 34479.60 with a percentage change of +0.47%.\\n\\n3. **NASDAQ**: The NASDAQ Composite is a broad-based capitalization-weighted index of stocks in all three NASDAQ tiers: Global Select, Global Market and Capital Market. Its current value is 13691.30 with a percentage change of +0.90%.\\n\\nThis information can help you make informed decisions about your investment plans.\"},\n",
       "  {'from': 'user',\n",
       "   'value': 'Could you explain what SEC filings are and how they might be useful for an investor like me?'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"Sure, SEC filings refer to official documents that are submitted by publicly traded companies to the U.S. Securities and Exchange Commission (SEC). These documents provide valuable information about a company's financial performance, its management team, ownership, key business operations, market competition, and future prospects. They are typically comprehensive and contain detailed information that a company is legally required to disclose to the public.\\n\\nInvestors like you can use these filings to better understand a company's true financial health and operations. Information you might glean from SEC filings includes:\\n\\n1. **Financial Statements**: Balance sheets, income statements, and cash flow statements can help you analyze the financial health and profitability of a company.\\n\\n2. **Management Discussion and Analysis (MD&A)**: This section offers management's perspective on the company's performance, including explanations of financial results, significant changes, market risk exposures, and future operations and strategy.\\n\\n3. **Risk Factors**: In the filing, companies are required to disclose potential risks of investing in their company. Insight into the risks the company is facing can help you make a more informed decision about investing.\\n\\n4. **Major Shareholders**: Companies must disclose the list of major shareholders. High ownership of shares by insiders could indicate confidence in the business, while large institutional ownership could suggest stability.\\n\\n5. **Director and Executive Compensation**: Information on executive remuneration packages can indicate how well the company’s interests are aligned with its executive team.\\n\\nSEC filings are a fundamental part of investment research and due diligence. They can provide deep insights into a company that may not be immediately obvious from simply looking at its earnings or share price. It's essential for investors to understand how to read and interpret these filings to make informed investment decisions.\"},\n",
       "  {'from': 'user',\n",
       "   'value': 'So, a couple of years ago, I invested in a tech startup without doing any proper research. It seemed like a hot trend at the time, but the company went belly up within a year. I ended up losing a good chunk of money. What are your thoughts on that?'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"As an assistant, I'm here to provide advice and guide you in your financial decisions. Here are my thoughts:\\n\\nInvesting in startups or any company without conducting proper research carries high risk. Although some startups have very high growth potentials and can provide excellent returns, many also fail within a few years of operation. This is especially the case in the tech industry where competition is stiff and technology changes rapidly.\\n\\nThat being said, your past experience with the tech startup is an important lesson in the necessity of conducting thorough due diligence before investing. Here are some steps you might take before future investments:\\n\\n1. **Understand the Business**: Make sure you understand the company's business model, its products or services, the market it operates in and its competition.\\n\\n2. **Financial Analysis**: Review financial statements, revenue, profitability, growth rates, debt levels, and cash flow. \\n\\n3. **Management Review**: It's important to assess the quality of the management team and their plans for the company.\\n\\n4. **Use of Tools**: Utilize tools like the SEC Filings tool to retrieve important documents to get detailed information about a company's operations, management and financials.\\n\\n5. **Risk vs Reward**: Analyze the investment's potential returns in light of the potential risk. High-risk investments should have the potential for higher returns.\\n\\nRemember, all investments carry a degree of risk. As such, it can be beneficial to diversify investments to spread risk and not put all of your money into one company or sector. It may also be helpful to seek advice from financial advisors or use trusted financial services to ensure informed decisions.\\n\\nGoing forward, employing these strategies could help reduce risk and maximize potential returns on your investments.\"},\n",
       "  {'from': 'user',\n",
       "   'value': \"Could you provide me with the SEC filings for Apple? Also, I’m curious about how COVID-19 has affected people's mobility away from home in the US.\"},\n",
       "  {'from': 'assistant',\n",
       "   'value': '[SEC Filings(identifier=\"AAPL\"), United States Away from Home Mobility API(string=\"2025-05-17\")]'}],\n",
       " 'cot_conversations': [{'from': 'system',\n",
       "   'value': 'You are ToolLlama, whenever asked a question, requiring tool call you responed after thinking and returning tool call within <tool></tool> XML tags.\\nYou are an expert in composing functions. You are given a question and a set of possible functions. \\nBased on the question, you will need to make one or more function/tool calls to achieve the purpose. \\nIf none of the function can be used, point it out. If the given question lacks the parameters required by the function,\\nalso point it out. .\\nHere is a list of functions in JSON format that you can invoke:\\n[{\"name\": \"newAddress\", \"description\": \"Generates a new Ethereum address that can be used to send or receive funds. Do not lose the password! We can\\'t restore access to an address if you lose it.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"password\": {\"description\": \"The password for the new Ethereum address\", \"type\": \"string\"}}, \"required\": [\"password\"]}, \"required\": null}, {\"name\": \"Market Trends API\", \"description\": \"Get the latest market trends and relevant news for a specified country and language.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"trend_type\": {\"description\": \"Trend type.\", \"type\": \"string\", \"enum\": [\"MARKET_INDEXES\", \"MOST_ACTIVE\", \"GAINERS\", \"LOSERS\", \"CRYPTO\", \"CURRENCIES\", \"CLIMATE_LEADERS\"]}, \"country\": {\"description\": \"The country for which to get trends, specified as a 2-letter country code - see ISO 3166.\", \"type\": \"string\", \"default\": \"us\"}, \"language\": {\"description\": \"The language to use for the results, specified as a 2-letter language code - see ISO 639-1.\", \"type\": \"string\", \"default\": \"en\"}}, \"required\": [\"trend_type\"]}, \"required\": null}, {\"name\": \"Get Futures Prices\", \"description\": \"Retrieve a list of current futures prices for various financial instruments.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"instrument_type\": {\"description\": \"Type of financial instrument (e.g., commodity, currency, index)\", \"type\": \"string\"}, \"exchange\": {\"description\": \"Name of the exchange (e.g., CME, ICE, NYMEX)\", \"type\": \"string\"}, \"start_string\": {\"description\": \"Start string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}, \"end_string\": {\"description\": \"End string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}}, \"required\": [\"instrument_type\", \"exchange\"]}, \"required\": null}, {\"name\": \"SEC Filings\", \"description\": \"Returns a list of SEC Filings for the requested company, including filing types, strings, and documents.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"identifier\": {\"description\": \"Publicly traded company\\'s stock symbol or Central Index Key (CIK)\", \"type\": \"string\", \"default\": \"aapl\"}}, \"required\": [\"identifier\"]}, \"required\": null}, {\"name\": \"United States Away from Home Mobility API\", \"description\": \"Retrieve daily data on the percentage change in time spent away from home in the United States, providing insights into the economic impact of the COVID-19 pandemic.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"string\": {\"description\": \"The string for which to retrieve data (format: YYYY-MM-DD)\", \"type\": \"string\"}, \"state\": {\"description\": \"The state for which to retrieve data (optional, default: all states)\", \"type\": \"string\"}}, \"required\": [\"string\"]}, \"required\": null}, {\"name\": \"Calculate Sales Tax\", \"description\": \"Retrieves the sales tax rate applicable to a specific address. This API accepts address inputs to deliver up-to-string, relevant local sales tax rates instantly.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"country\": {\"description\": \"Set to one of the country codes listed in Supported Countries.\", \"type\": \"string\", \"default\": \"US\"}, \"city\": {\"description\": \"City name\", \"type\": \"string\", \"default\": \"Meridian\"}, \"zip\": {\"description\": \"Zip code\", \"type\": \"string\", \"default\": \"83646\"}, \"street\": {\"description\": \"Street address\", \"type\": \"string\", \"default\": \"936 Storey Ave\"}}, \"required\": [\"country\"]}, \"required\": null}]. \\nShould you decide to return the function call(s). \\nPut it in the format of [func1(params_name=params_value, params_name2=params_value2...), func2(params)]\\n\\n. \\n'},\n",
       "  {'from': 'user',\n",
       "   'value': \"I'm considering investing and I'd like to know what's happening in the market right now. Could you get me the top market trends in the US?\"},\n",
       "  {'from': 'assistant',\n",
       "   'value': 'To get the top market trends in the US, I need to consider the current market situation and the types of trends that are typically available. The Market Trends API seems like the most suitable tool for this task, as it provides the latest market trends and relevant news for a specified country and language. I will use the Market Trends API with the trend type set to MARKET_INDEXES and the country set to us. My thought process is as follows: \\n1. Identify the goal: Get the top market trends in the US.\\n2. Choose the tool: Market Trends API.\\n3. Set the parameters: trend_type = MARKET_INDEXES, country = us.\\nWith these parameters, I can make the API call to get the required information. <tool>[Market Trends API(trend_type=\"MARKET_INDEXES\", country=\"us\")]</tool>'},\n",
       "  {'from': 'tool',\n",
       "   'value': '[{\"name\": \"Market Trends API\", \"results\": {\"trends\": [{\"name\": \"S&P 500\", \"description\": \"Standard & Poor\\'s 500 Index is a market-capitalization-weighted index of the 500 largest U.S. publicly traded companies.\", \"data\": {\"current_value\": \"4172.80\", \"percentage_change\": \"+0.68%\"}}, {\"name\": \"DOW J\", \"description\": \"Dow Jones Industrial Average is a price-weighted average of 30 blue-chip stocks that are generally the leaders in their industry.\", \"data\": {\"current_value\": \"34479.60\", \"percentage_change\": \"+0.47%\"}}, {\"name\": \"NASDAQ\", \"description\": \"The NASDAQ Composite is a broad-based capitalization-weighted index of stocks in all three NASDAQ tiers: Global Select, Global Market and Capital Market.\", \"data\": {\"current_value\": \"13691.30\", \"percentage_change\": \"+0.90%\"}}]}}]'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"Here are the top Market Trends in the US right now:\\n\\n1. **S&P 500**: The Standard & Poor's 500 Index is a market-capitalization-weighted index of the 500 largest U.S. publicly traded companies. Its current value is 4172.80 with a percentage change of +0.68%.\\n\\n2. **DOW J**: The Dow Jones Industrial Average is a price-weighted average of 30 blue-chip stocks that are generally the leaders in their industry. Its current value is 34479.60 with a percentage change of +0.47%.\\n\\n3. **NASDAQ**: The NASDAQ Composite is a broad-based capitalization-weighted index of stocks in all three NASDAQ tiers: Global Select, Global Market and Capital Market. Its current value is 13691.30 with a percentage change of +0.90%.\\n\\nThis information can help you make informed decisions about your investment plans.\"},\n",
       "  {'from': 'user',\n",
       "   'value': 'Could you explain what SEC filings are and how they might be useful for an investor like me?'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"Sure, SEC filings refer to official documents that are submitted by publicly traded companies to the U.S. Securities and Exchange Commission (SEC). These documents provide valuable information about a company's financial performance, its management team, ownership, key business operations, market competition, and future prospects. They are typically comprehensive and contain detailed information that a company is legally required to disclose to the public.\\n\\nInvestors like you can use these filings to better understand a company's true financial health and operations. Information you might glean from SEC filings includes:\\n\\n1. **Financial Statements**: Balance sheets, income statements, and cash flow statements can help you analyze the financial health and profitability of a company.\\n\\n2. **Management Discussion and Analysis (MD&A)**: This section offers management's perspective on the company's performance, including explanations of financial results, significant changes, market risk exposures, and future operations and strategy.\\n\\n3. **Risk Factors**: In the filing, companies are required to disclose potential risks of investing in their company. Insight into the risks the company is facing can help you make a more informed decision about investing.\\n\\n4. **Major Shareholders**: Companies must disclose the list of major shareholders. High ownership of shares by insiders could indicate confidence in the business, while large institutional ownership could suggest stability.\\n\\n5. **Director and Executive Compensation**: Information on executive remuneration packages can indicate how well the company’s interests are aligned with its executive team.\\n\\nSEC filings are a fundamental part of investment research and due diligence. They can provide deep insights into a company that may not be immediately obvious from simply looking at its earnings or share price. It's essential for investors to understand how to read and interpret these filings to make informed investment decisions.\"},\n",
       "  {'from': 'user',\n",
       "   'value': 'So, a couple of years ago, I invested in a tech startup without doing any proper research. It seemed like a hot trend at the time, but the company went belly up within a year. I ended up losing a good chunk of money. What are your thoughts on that?'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"As an assistant, I'm here to provide advice and guide you in your financial decisions. Here are my thoughts:\\n\\nInvesting in startups or any company without conducting proper research carries high risk. Although some startups have very high growth potentials and can provide excellent returns, many also fail within a few years of operation. This is especially the case in the tech industry where competition is stiff and technology changes rapidly.\\n\\nThat being said, your past experience with the tech startup is an important lesson in the necessity of conducting thorough due diligence before investing. Here are some steps you might take before future investments:\\n\\n1. **Understand the Business**: Make sure you understand the company's business model, its products or services, the market it operates in and its competition.\\n\\n2. **Financial Analysis**: Review financial statements, revenue, profitability, growth rates, debt levels, and cash flow. \\n\\n3. **Management Review**: It's essential to assess the quality of the management team and their plans for the company.\\n\\n4. **Use of Tools**: Utilize tools like the SEC Filings tool to retrieve important documents to get detailed information about a company's operations, management and financials.\\n\\n5. **Risk vs Reward**: Analyze the investment's potential returns in light of the potential risk. High-risk investments should have the potential for higher returns.\\n\\nRemember, all investments carry a degree of risk. As such, it can be beneficial to diversify investments to spread risk and not put all of your money into one company or sector. It may also be helpful to seek advice from financial advisors or use trusted financial services to ensure informed decisions.\\n\\nGoing forward, employing these strategies could help reduce risk and maximize potential returns on your investments.\"},\n",
       "  {'from': 'user',\n",
       "   'value': \"Could you provide me with the SEC filings for Apple? Also, I’m curious about how COVID-19 has affected people's mobility away from home in the US.\"},\n",
       "  {'from': 'assistant',\n",
       "   'value': 'To address your request, I need to consider the tools available to me. For the SEC filings of Apple, the SEC Filings tool is the most suitable, and for the information on COVID-19\\'s impact on people\\'s mobility, the United States Away from Home Mobility API is the best choice. My thought process is as follows: \\n1. Identify the goals: Get the SEC filings for Apple and understand the impact of COVID-19 on people\\'s mobility in the US.\\n2. Choose the tools: SEC Filings for Apple\\'s financials and United States Away from Home Mobility API for COVID-19 impact.\\n3. Set the parameters: For SEC Filings, identifier = AAPL; for United States Away from Home Mobility API, string = 2025-05-17.\\nWith these parameters, I can make the API calls to get the required information. <tool>[SEC Filings(identifier=\"AAPL\"), United States Away from Home Mobility API(string=\"2025-05-17\")]</tool>'}]}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cot['train'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a86e7059-8d13-4bb2-8377-099f33513b9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dcca6c52816a4ff5928f72d1f7e45cbe",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Transforming dataset (num_proc=16):   0%|          | 0/11300 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "861a15c038f14e0c89c3c787c4250f7d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Saving the dataset (0/1 shards):   0%|          | 0/11300 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ID: 0f3721b9-6082-4635-8170-71dd61ebedc9\n",
      "First message: {'from': 'system', 'value': 'You are ToolLlama, whenever asked a question, requiring tool call you responed after thinking and returning tool call within <tool></tool> XML tags.\\nYou are an expert in composing functions. You are given a question and a set of possible functions. \\nBased on the question, you will need to make one or more function/tool calls to achieve the purpose. \\nIf none of the function can be used, point it out. If the given question lacks the parameters required by the function,\\nalso point it out. You should only return the function call in tools call sections.\\nHere is a list of functions in JSON format that you can invoke:\\n[{\"name\": \"newAddress\", \"description\": \"Generates a new Ethereum address that can be used to send or receive funds. Do not lose the password! We can\\'t restore access to an address if you lose it.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"password\": {\"description\": \"The password for the new Ethereum address\", \"type\": \"string\"}}, \"required\": [\"password\"]}, \"required\": null}, {\"name\": \"Market Trends API\", \"description\": \"Get the latest market trends and relevant news for a specified country and language.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"trend_type\": {\"description\": \"Trend type.\", \"type\": \"string\", \"enum\": [\"MARKET_INDEXES\", \"MOST_ACTIVE\", \"GAINERS\", \"LOSERS\", \"CRYPTO\", \"CURRENCIES\", \"CLIMATE_LEADERS\"]}, \"country\": {\"description\": \"The country for which to get trends, specified as a 2-letter country code - see ISO 3166.\", \"type\": \"string\", \"default\": \"us\"}, \"language\": {\"description\": \"The language to use for the results, specified as a 2-letter language code - see ISO 639-1.\", \"type\": \"string\", \"default\": \"en\"}}, \"required\": [\"trend_type\"]}, \"required\": null}, {\"name\": \"Get Futures Prices\", \"description\": \"Retrieve a list of current futures prices for various financial instruments.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"instrument_type\": {\"description\": \"Type of financial instrument (e.g., commodity, currency, index)\", \"type\": \"string\"}, \"exchange\": {\"description\": \"Name of the exchange (e.g., CME, ICE, NYMEX)\", \"type\": \"string\"}, \"start_string\": {\"description\": \"Start string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}, \"end_string\": {\"description\": \"End string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}}, \"required\": [\"instrument_type\", \"exchange\"]}, \"required\": null}, {\"name\": \"SEC Filings\", \"description\": \"Returns a list of SEC Filings for the requested company, including filing types, strings, and documents.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"identifier\": {\"description\": \"Publicly traded company\\'s stock symbol or Central Index Key (CIK)\", \"type\": \"string\", \"default\": \"aapl\"}}, \"required\": [\"identifier\"]}, \"required\": null}, {\"name\": \"United States Away from Home Mobility API\", \"description\": \"Retrieve daily data on the percentage change in time spent away from home in the United States, providing insights into the economic impact of the COVID-19 pandemic.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"string\": {\"description\": \"The string for which to retrieve data (format: YYYY-MM-DD)\", \"type\": \"string\"}, \"state\": {\"description\": \"The state for which to retrieve data (optional, default: all states)\", \"type\": \"string\"}}, \"required\": [\"string\"]}, \"required\": null}, {\"name\": \"Calculate Sales Tax\", \"description\": \"Retrieves the sales tax rate applicable to a specific address. This API accepts address inputs to deliver up-to-string, relevant local sales tax rates instantly.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"country\": {\"description\": \"Set to one of the country codes listed in Supported Countries.\", \"type\": \"string\", \"default\": \"US\"}, \"city\": {\"description\": \"City name\", \"type\": \"string\", \"default\": \"Meridian\"}, \"zip\": {\"description\": \"Zip code\", \"type\": \"string\", \"default\": \"83646\"}, \"street\": {\"description\": \"Street address\", \"type\": \"string\", \"default\": \"936 Storey Ave\"}}, \"required\": [\"country\"]}, \"required\": null}]. \\nShould you decide to return the function call(s). \\nPut it in the format of [func1(params_name=params_value, params_name2=params_value2...), func2(params)]\\n\\nNO other text MUST be included. \\n'}\n"
     ]
    }
   ],
   "source": [
    "from datasets import load_dataset\n",
    "import multiprocessing\n",
    "import uuid\n",
    "\n",
    "# Load the ToolACE dataset\n",
    "dataset = load_dataset(\"Team-ACE/ToolACE\")\n",
    "\n",
    "# Define the transformation function for batched processing\n",
    "def transform_examples(examples, indices):\n",
    "    tool_llama_prefix = \"You are ToolLlama, whenever asked a question, requiring tool call you responed after thinking and returning tool call within <tool></tool> XML tags.\\n\"\n",
    "    \n",
    "    # Initialize the output lists\n",
    "    ids = []\n",
    "    new_conversations_list = []\n",
    "    \n",
    "    # Process each example in the batch\n",
    "    for i in range(len(indices)):\n",
    "        # Generate a unique ID\n",
    "        unique_id = str(uuid.uuid4())\n",
    "        ids.append(unique_id)\n",
    "        \n",
    "        # Extract and modify system message content\n",
    "        if isinstance(examples['system'][i], str):\n",
    "            system_content = tool_llama_prefix + examples['system'][i]\n",
    "        elif isinstance(examples['system'][i], dict) and 'value' in examples['system'][i]:\n",
    "            system_content = tool_llama_prefix + examples['system'][i]['value']\n",
    "        else:\n",
    "            system_content = tool_llama_prefix + str(examples['system'][i])\n",
    "        \n",
    "        # Create new conversations array with system message as first item\n",
    "        new_conversations = [\n",
    "            {\n",
    "                \"from\": \"system\",\n",
    "                \"value\": system_content\n",
    "            }\n",
    "        ]\n",
    "        \n",
    "        # Add the rest of the conversation messages\n",
    "        new_conversations.extend(examples['conversations'][i])\n",
    "        \n",
    "        # Wrap function calls with <tool></tool> tags\n",
    "        for j in range(len(new_conversations)):\n",
    "            # Check if current message is from assistant\n",
    "            if new_conversations[j][\"from\"] == \"assistant\":\n",
    "                assistant_value = new_conversations[j][\"value\"]\n",
    "                \n",
    "                # Case 1: Assistant message followed by tool response\n",
    "                if (j+1 < len(new_conversations) and new_conversations[j+1][\"from\"] == \"tool\"):\n",
    "                    if assistant_value.strip().startswith('[') and assistant_value.strip().endswith(']'):\n",
    "                        new_conversations[j][\"value\"] = f\"<tool>{assistant_value}</tool>\"\n",
    "                \n",
    "                # Case 2: Assistant message is the last message\n",
    "                elif j == len(new_conversations) - 1:\n",
    "                    if assistant_value.strip().startswith('[') and assistant_value.strip().endswith(']'):\n",
    "                        new_conversations[j][\"value\"] = f\"<tool>{assistant_value}</tool>\"\n",
    "        \n",
    "        new_conversations_list.append(new_conversations)\n",
    "    \n",
    "    # Return the batch of transformed examples\n",
    "    return {\n",
    "        \"id\": ids,\n",
    "        \"conversations\": new_conversations_list\n",
    "    }\n",
    "\n",
    "# Process with parallelism\n",
    "def process_dataset():\n",
    "    # Use transform_examples for batched processing\n",
    "    return dataset.map(\n",
    "        function=transform_examples,\n",
    "        with_indices=True,\n",
    "        batched=True,\n",
    "        batch_size=100,  # Process in larger batches\n",
    "        num_proc=multiprocessing.cpu_count(),  # Use all available CPU cores\n",
    "        desc=\"Transforming dataset\",  # Add progress description\n",
    "        remove_columns=[\"system\"]  # Remove the old system column\n",
    "    )\n",
    "\n",
    "# Run the transformation\n",
    "transformed_dataset = process_dataset()\n",
    "\n",
    "# Save the transformed dataset\n",
    "transformed_dataset.save_to_disk(\"toolllama_formatted_dataset\")\n",
    "\n",
    "# Optional: Print an example to verify the transformation\n",
    "if len(transformed_dataset[\"train\"]) > 0:\n",
    "    example = transformed_dataset[\"train\"][0]\n",
    "    print(f\"ID: {example['id']}\")\n",
    "    print(f\"First message: {example['conversations'][0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "71d9af93-1dbd-4b62-9afc-c8b50c18c25f",
   "metadata": {},
   "outputs": [],
   "source": [
    "test = load_from_disk(\"./toolllama_formatted_dataset/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "927c8154-015b-49be-9d02-f9aef1fb3551",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['conversations', 'id'],\n",
       "    num_rows: 11300\n",
       "})"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test['train']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1970ca28-90c3-44c8-91f9-3e006f6284f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'conversations': [{'from': 'system',\n",
       "   'value': 'You are ToolLlama, whenever asked a question, requiring tool call you responed after thinking and returning tool call within <tool></tool> XML tags.\\nYou are an expert in composing functions. You are given a question and a set of possible functions. \\nBased on the question, you will need to make one or more function/tool calls to achieve the purpose. \\nIf none of the function can be used, point it out. If the given question lacks the parameters required by the function,\\nalso point it out. You should only return the function call in tools call sections.\\nHere is a list of functions in JSON format that you can invoke:\\n[{\"name\": \"newAddress\", \"description\": \"Generates a new Ethereum address that can be used to send or receive funds. Do not lose the password! We can\\'t restore access to an address if you lose it.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"password\": {\"description\": \"The password for the new Ethereum address\", \"type\": \"string\"}}, \"required\": [\"password\"]}, \"required\": null}, {\"name\": \"Market Trends API\", \"description\": \"Get the latest market trends and relevant news for a specified country and language.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"trend_type\": {\"description\": \"Trend type.\", \"type\": \"string\", \"enum\": [\"MARKET_INDEXES\", \"MOST_ACTIVE\", \"GAINERS\", \"LOSERS\", \"CRYPTO\", \"CURRENCIES\", \"CLIMATE_LEADERS\"]}, \"country\": {\"description\": \"The country for which to get trends, specified as a 2-letter country code - see ISO 3166.\", \"type\": \"string\", \"default\": \"us\"}, \"language\": {\"description\": \"The language to use for the results, specified as a 2-letter language code - see ISO 639-1.\", \"type\": \"string\", \"default\": \"en\"}}, \"required\": [\"trend_type\"]}, \"required\": null}, {\"name\": \"Get Futures Prices\", \"description\": \"Retrieve a list of current futures prices for various financial instruments.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"instrument_type\": {\"description\": \"Type of financial instrument (e.g., commodity, currency, index)\", \"type\": \"string\"}, \"exchange\": {\"description\": \"Name of the exchange (e.g., CME, ICE, NYMEX)\", \"type\": \"string\"}, \"start_string\": {\"description\": \"Start string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}, \"end_string\": {\"description\": \"End string for the price data (YYYY-MM-DD)\", \"type\": \"string\"}}, \"required\": [\"instrument_type\", \"exchange\"]}, \"required\": null}, {\"name\": \"SEC Filings\", \"description\": \"Returns a list of SEC Filings for the requested company, including filing types, strings, and documents.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"identifier\": {\"description\": \"Publicly traded company\\'s stock symbol or Central Index Key (CIK)\", \"type\": \"string\", \"default\": \"aapl\"}}, \"required\": [\"identifier\"]}, \"required\": null}, {\"name\": \"United States Away from Home Mobility API\", \"description\": \"Retrieve daily data on the percentage change in time spent away from home in the United States, providing insights into the economic impact of the COVID-19 pandemic.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"string\": {\"description\": \"The string for which to retrieve data (format: YYYY-MM-DD)\", \"type\": \"string\"}, \"state\": {\"description\": \"The state for which to retrieve data (optional, default: all states)\", \"type\": \"string\"}}, \"required\": [\"string\"]}, \"required\": null}, {\"name\": \"Calculate Sales Tax\", \"description\": \"Retrieves the sales tax rate applicable to a specific address. This API accepts address inputs to deliver up-to-string, relevant local sales tax rates instantly.\", \"parameters\": {\"type\": \"dict\", \"properties\": {\"country\": {\"description\": \"Set to one of the country codes listed in Supported Countries.\", \"type\": \"string\", \"default\": \"US\"}, \"city\": {\"description\": \"City name\", \"type\": \"string\", \"default\": \"Meridian\"}, \"zip\": {\"description\": \"Zip code\", \"type\": \"string\", \"default\": \"83646\"}, \"street\": {\"description\": \"Street address\", \"type\": \"string\", \"default\": \"936 Storey Ave\"}}, \"required\": [\"country\"]}, \"required\": null}]. \\nShould you decide to return the function call(s). \\nPut it in the format of [func1(params_name=params_value, params_name2=params_value2...), func2(params)]\\n\\nNO other text MUST be included. \\n'},\n",
       "  {'from': 'user',\n",
       "   'value': \"I'm considering investing and I'd like to know what's happening in the market right now. Could you get me the top market trends in the US?\"},\n",
       "  {'from': 'assistant',\n",
       "   'value': '<tool>[Market Trends API(trend_type=\"MARKET_INDEXES\", country=\"us\")]</tool>'},\n",
       "  {'from': 'tool',\n",
       "   'value': '[{\"name\": \"Market Trends API\", \"results\": {\"trends\": [{\"name\": \"S&P 500\", \"description\": \"Standard & Poor\\'s 500 Index is a market-capitalization-weighted index of the 500 largest U.S. publicly traded companies.\", \"data\": {\"current_value\": \"4172.80\", \"percentage_change\": \"+0.68%\"}}, {\"name\": \"DOW J\", \"description\": \"Dow Jones Industrial Average is a price-weighted average of 30 blue-chip stocks that are generally the leaders in their industry.\", \"data\": {\"current_value\": \"34479.60\", \"percentage_change\": \"+0.47%\"}}, {\"name\": \"NASDAQ\", \"description\": \"The NASDAQ Composite is a broad-based capitalization-weighted index of stocks in all three NASDAQ tiers: Global Select, Global Market and Capital Market.\", \"data\": {\"current_value\": \"13691.30\", \"percentage_change\": \"+0.90%\"}}]}}]'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"Here are the top Market Trends in the US right now:\\n\\n1. **S&P 500**: The Standard & Poor's 500 Index is a market-capitalization-weighted index of the 500 largest U.S. publicly traded companies. Its current value is 4172.80 with a percentage change of +0.68%.\\n\\n2. **DOW J**: The Dow Jones Industrial Average is a price-weighted average of 30 blue-chip stocks that are generally the leaders in their industry. Its current value is 34479.60 with a percentage change of +0.47%.\\n\\n3. **NASDAQ**: The NASDAQ Composite is a broad-based capitalization-weighted index of stocks in all three NASDAQ tiers: Global Select, Global Market and Capital Market. Its current value is 13691.30 with a percentage change of +0.90%.\\n\\nThis information can help you make informed decisions about your investment plans.\"},\n",
       "  {'from': 'user',\n",
       "   'value': 'Could you explain what SEC filings are and how they might be useful for an investor like me?'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"Sure, SEC filings refer to official documents that are submitted by publicly traded companies to the U.S. Securities and Exchange Commission (SEC). These documents provide valuable information about a company's financial performance, its management team, ownership, key business operations, market competition, and future prospects. They are typically comprehensive and contain detailed information that a company is legally required to disclose to the public.\\n\\nInvestors like you can use these filings to better understand a company's true financial health and operations. Information you might glean from SEC filings includes:\\n\\n1. **Financial Statements**: Balance sheets, income statements, and cash flow statements can help you analyze the financial health and profitability of a company.\\n\\n2. **Management Discussion and Analysis (MD&A)**: This section offers management's perspective on the company's performance, including explanations of financial results, significant changes, market risk exposures, and future operations and strategy.\\n\\n3. **Risk Factors**: In the filing, companies are required to disclose potential risks of investing in their company. Insight into the risks the company is facing can help you make a more informed decision about investing.\\n\\n4. **Major Shareholders**: Companies must disclose the list of major shareholders. High ownership of shares by insiders could indicate confidence in the business, while large institutional ownership could suggest stability.\\n\\n5. **Director and Executive Compensation**: Information on executive remuneration packages can indicate how well the company’s interests are aligned with its executive team.\\n\\nSEC filings are a fundamental part of investment research and due diligence. They can provide deep insights into a company that may not be immediately obvious from simply looking at its earnings or share price. It's essential for investors to understand how to read and interpret these filings to make informed investment decisions.\"},\n",
       "  {'from': 'user',\n",
       "   'value': 'So, a couple of years ago, I invested in a tech startup without doing any proper research. It seemed like a hot trend at the time, but the company went belly up within a year. I ended up losing a good chunk of money. What are your thoughts on that?'},\n",
       "  {'from': 'assistant',\n",
       "   'value': \"As an assistant, I'm here to provide advice and guide you in your financial decisions. Here are my thoughts:\\n\\nInvesting in startups or any company without conducting proper research carries high risk. Although some startups have very high growth potentials and can provide excellent returns, many also fail within a few years of operation. This is especially the case in the tech industry where competition is stiff and technology changes rapidly.\\n\\nThat being said, your past experience with the tech startup is an important lesson in the necessity of conducting thorough due diligence before investing. Here are some steps you might take before future investments:\\n\\n1. **Understand the Business**: Make sure you understand the company's business model, its products or services, the market it operates in and its competition.\\n\\n2. **Financial Analysis**: Review financial statements, revenue, profitability, growth rates, debt levels, and cash flow. \\n\\n3. **Management Review**: It's important to assess the quality of the management team and their plans for the company.\\n\\n4. **Use of Tools**: Utilize tools like the SEC Filings tool to retrieve important documents to get detailed information about a company's operations, management and financials.\\n\\n5. **Risk vs Reward**: Analyze the investment's potential returns in light of the potential risk. High-risk investments should have the potential for higher returns.\\n\\nRemember, all investments carry a degree of risk. As such, it can be beneficial to diversify investments to spread risk and not put all of your money into one company or sector. It may also be helpful to seek advice from financial advisors or use trusted financial services to ensure informed decisions.\\n\\nGoing forward, employing these strategies could help reduce risk and maximize potential returns on your investments.\"},\n",
       "  {'from': 'user',\n",
       "   'value': \"Could you provide me with the SEC filings for Apple? Also, I’m curious about how COVID-19 has affected people's mobility away from home in the US.\"},\n",
       "  {'from': 'assistant',\n",
       "   'value': '<tool>[SEC Filings(identifier=\"AAPL\"), United States Away from Home Mobility API(string=\"2025-05-17\")]</tool>'}],\n",
       " 'id': '0f3721b9-6082-4635-8170-71dd61ebedc9'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test['train'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bad74e6-74d0-4344-af8e-878214f22acf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import random\n",
    "from pathlib import Path\n",
    "\n",
    "# Create a directory for saving the tool example files \n",
    "os.makedirs(\\\"../../tool_examples\\\", exist_ok=True)\n",
    "\n",
    "# Number of examples to use in the sample\n",
    "NUM_EXAMPLES = 10\n",
    "\n",
    "# Get a sample of conversations from the dataset\n",
    "sample_indices = random.sample(range(len(test['train'])), NUM_EXAMPLES)\n",
    "sample_conversations = [test['train'][i] for i in sample_indices]\n",
    "\n",
    "# Format for cot-enhance: Each conversation should be in the expected structure\n",
    "formatted_conversations = []\n",
    "\n",
    "for example in sample_conversations:\n",
    "    # Each example is already in the right format with XML tags for tool calls\n",
    "    formatted_conversations.append({\n",
    "        \\\"conversations\\\": example[\\\"conversations\\\"]\n",
    "    })\n",
    "\n",
    "# Now save this as a JSON file in format that cot-enhance expects\n",
    "# For multiple conversations, we'll create a list of conversation objects\n",
    "output_file = \\\"../../tool_examples/multi_conversations.json\\\"\n",
    "with open(output_file, 'w', encoding='utf-8') as f:\n",
    "    json.dump(formatted_conversations, f, indent=2)\n",
    "\n",
    "print(f\\\"Sample of {NUM_EXAMPLES} conversations saved to {output_file}\\\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now let's test the cot-enhance feature with our generated file\n",
    "# First, we'll create the custom config file for cot-enhance\n",
    "\n",
    "import yaml\n",
    "import os\n",
    "\n",
    "# Create the config directory if it doesn't exist\n",
    "config_dir = os.path.dirname(os.path.abspath(\\\"cot_tools_config.yaml\\\"))\n",
    "os.makedirs(config_dir, exist_ok=True)\n",
    "\n",
    "# Custom config for CoT enhancement\n",
    "cot_config = {\n",
    "    \\\"vllm\\\": {\n",
    "        \\\"api_base\\\": \\\"http://localhost:8000/v1\\\",\n",
    "        \\\"model\\\": \\\"unsloth/Meta-Llama-3.1-8B-Instruct\\\",\n",
    "        \\\"max_retries\\\": 3,\n",
    "        \\\"retry_delay\\\": 1.0\n",
    "    },\n",
    "    \\\"generation\\\": {\n",
    "        \\\"temperature\\\": 0.2,\n",
    "        \\\"top_p\\\": 0.95,\n",
    "        \\\"max_tokens\\\": 8192\n",
    "    },\n",
    "    \\\"prompts\\\": {\n",
    "        \\\"cot_enhancement\\\": \\\"\\\"\\\"You are a high 170IQ reasoning super smart AI, your job is to enhance existing conversation examples. Remember return the entire conversation as is BUT\n",
    "\n",
    "BUT We are add Chain of Thought and planning to \"Assistant\" messages whenever it returns a tool call.\n",
    "\n",
    "Remember ONLY When it does return a tool, we all add thinking and reasoning Traces before it to add logic otherwise we don't touch the conversation history\n",
    "\n",
    "Remember to return the entire message but only enhance the assistant messages whenever it calls a tool with thoghts\n",
    "\n",
    "Please keep in mind we are not modifying anything in the example neither are we changing what it does, only add CoT everytime a tool gets called in the conversation\n",
    "\n",
    "Think out loud and max out your tokens when adding CoT\\\"\\\"\\\"\n",
    "    }\n",
    "}\n",
    "\n",
    "# Save the config file\n",
    "with open(\\\"cot_tools_config.yaml\\\", 'w') as f:\n",
    "    yaml.dump(cot_config, f, default_flow_style=False, sort_keys=False)\n",
    "\n",
    "print(\\\"Custom cot-enhance config file saved to cot_tools_config.yaml\\\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now we'll run the cot-enhance command on our sample file\n",
    "# This is how we would run it from the command line\n",
    "# synthetic-data-kit -c cot_tools_config.yaml create ../../tool_examples/multi_conversations.json --type cot-enhance -o ../../enhanced_results/\n",
    "\n",
    "# In this notebook, we'll test it by importing and calling the function directly\n",
    "# This is just for demonstration purposes - in real usage, you'd use the CLI command\n",
    "\n",
    "import os\n",
    "from synthetic_data_kit.core.create import process_file\n",
    "from pathlib import Path\n",
    "\n",
    "# Create the output directory\n",
    "os.makedirs(\\\"../../enhanced_results\\\", exist_ok=True)\n",
    "\n",
    "# Call the function directly (similar to what the CLI does)\n",
    "input_file = \\\"../../tool_examples/multi_conversations.json\\\"\n",
    "output_dir = \\\"../../enhanced_results\\\"\n",
    "config_path = Path(\\\"cot_tools_config.yaml\\\").absolute()\n",
    "content_type = \\\"cot-enhance\\\"\n",
    "verbose = True\n",
    "\n",
    "# This simulates what happens when you run the CLI command\n",
    "try:\n",
    "    output_path = process_file(\n",
    "        file_path=input_file,\n",
    "        output_dir=output_dir,\n",
    "        config_path=config_path,\n",
    "        api_base=None,  # Will use config file setting\n",
    "        model=None,     # Will use config file setting\n",
    "        content_type=content_type,\n",
    "        num_pairs=None,\n",
    "        verbose=verbose\n",
    "    )\n",
    "    print(f\\\"Enhanced conversations saved to: {output_path}\\\")\n",
    "    \n",
    "    # Let's look at one example of the enhanced conversation\n",
    "    # We'll load the enhanced file and compare it to the original\n",
    "    import json\n",
    "    \n",
    "    with open(output_path, 'r') as f:\n",
    "        enhanced_data = json.load(f)\n",
    "    \n",
    "    if isinstance(enhanced_data, list):\n",
    "        # We have multiple conversations\n",
    "        print(f\\\"Enhanced {len(enhanced_data)} conversations\\\")\n",
    "        # Let's examine the first conversation\n",
    "        print(\\\"\\\\nFirst Enhanced Conversation Sample:\\\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "        # Let's examine a tool call from the first conversation\n",
    "        for i, conv in enumerate(enhanced_data):\n",
    "            conversations = conv.get(\\\"conversations\\\", [])\n",
    "            for j, message in enumerate(conversations):\n",
    "                if message.get(\\\"from\\\") == \\\"assistant\\\" and \\\"<tool>\\\" in message.get(\\\"value\\\", \\\"\\\"):\n",
    "                    print(f\\\"\\\\nBefore and After Enhancement Example (Conversation {i+1}, Message {j+1}):\\\\n\\\")\n",
    "                    \n",
    "                    # Original version\n",
    "                    print(\\\"ORIGINAL (from original dataset):\\\")\n",
    "                    print(\\\"-\\\" * 80)\n",
    "                    original_tool_call = sample_conversations[i][\\\"conversations\\\"][j][\\\"value\\\"]\n",
    "                    print(original_tool_call)\n",
    "                    print(\\\"-\\\" * 80)\n",
    "                    \n",
    "                    # Enhanced version\n",
    "                    print(\\\"\\\\nENHANCED (with Chain of Thought):\\\")\n",
    "                    print(\\\"-\\\" * 80)\n",
    "                    enhanced_tool_call = message[\\\"value\\\"]\n",
    "                    print(enhanced_tool_call)\n",
    "                    print(\\\"-\\\" * 80)\n",
    "                    \n",
    "                    # Exit after first example\n",
    "                    print(\\\"\\\\nNote how the enhanced version includes detailed reasoning steps before making the tool call.\\\\n\\\")\n",
    "                    raise StopIteration\n",
    "    \n",
    "    elif isinstance(enhanced_data, dict) and \\\"conversations\\\" in enhanced_data:\n",
    "        # We have a single conversation\n",
    "        print(\\\"Enhanced a single conversation\\\")\n",
    "        for j, message in enumerate(enhanced_data[\\\"conversations\\\"]):\n",
    "            if message.get(\\\"from\\\") == \\\"assistant\\\" and \\\"<tool>\\\" in message.get(\\\"value\\\", \\\"\\\"):\n",
    "                print(f\\\"\\\\nBefore and After Enhancement Example (Message {j+1}):\\\\n\\\")\n",
    "                \n",
    "                # Original version\n",
    "                print(\\\"ORIGINAL (from original dataset):\\\")\n",
    "                print(\\\"-\\\" * 80)\n",
    "                original_tool_call = sample_conversations[0][\\\"conversations\\\"][j][\\\"value\\\"]\n",
    "                print(original_tool_call)\n",
    "                print(\\\"-\\\" * 80)\n",
    "                \n",
    "                # Enhanced version\n",
    "                print(\\\"\\\\nENHANCED (with Chain of Thought):\\\")\n",
    "                print(\\\"-\\\" * 80)\n",
    "                enhanced_tool_call = message[\\\"value\\\"]\n",
    "                print(enhanced_tool_call)\n",
    "                print(\\\"-\\\" * 80)\n",
    "                \n",
    "                # Exit after first example\n",
    "                print(\\\"\\\\nNote how the enhanced version includes detailed reasoning steps before making the tool call.\\\\n\\\")\n",
    "                break\n",
    "except StopIteration:\n",
    "    # Used to break out of nested loops\n",
    "    pass\n",
    "except Exception as e:\n",
    "    print(f\\\"Error: {e}\\\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
